package com.example.allplan.Activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.ItemTouchHelper.JiHuaBaoTouchCallback;
import com.example.allplan.JavaBean.JiHuaTiaoJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.MyDialog.ShuRuJiHuaDialog;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.JiHuaTiaoRecyclerAdapter;
import com.example.allplan.ShuJuKuCaoZuo.JiHuaSQCaoZuo;

import java.nio.file.FileAlreadyExistsException;
import java.util.ArrayList;
import java.util.Collections;

public class JiHuaYeMianActivity extends BaseActivity {
    Toolbar mbiao_ti_toolbar;
    RecyclerView mkanjihualiebiaorecyclerview;
    ArrayList<JiHuaTiaoJavaBean> mlist;
    ArrayList<String> mslist;
    JiHuaTiaoRecyclerAdapter madapter;
    String wenjianming, biaotiming;
    int leixing;
    MyApplication myapp;
    JiHuaSQCaoZuo msq;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ji_hua_ye_mian_activity);
        init();
    }

    void fuzhimingzi() {
        for (int i = 0; i < mlist.size(); i++) {
            mslist.add(mlist.get(i).getMingcheng());
        }
    }

    void init() {
        biaotiming = getIntent().getStringExtra("biao_ti");
        wenjianming = getIntent().getStringExtra("wen_jian_ming");
        myapp = MyApplication.getInstance();
        mbiao_ti_toolbar = findViewById(R.id.kan_ji_hua_toolbar);
        mbiao_ti_toolbar.setTitle(biaotiming);
        setSupportActionBar(mbiao_ti_toolbar);
        mkanjihualiebiaorecyclerview = findViewById(R.id.kan_ji_hua_tiao_mu_recyclerview);
        mlist = new ArrayList<JiHuaTiaoJavaBean>();
        msq = new JiHuaSQCaoZuo(this, wenjianming);
        load();
    }
    void load(){
        mlist = msq.reloadAllJiHuaTiao();
        Collections.sort(mlist);
        mslist = new ArrayList<String>();
        fuzhimingzi();
        madapter = new JiHuaTiaoRecyclerAdapter(mlist, new JiHuaTiaoRecyclerAdapter.onItemClickListener() {
            @Override
            public void onClick(final int pos, Context mcon) {
                if(mlist.get(pos).getZan_cun_zhi()<=0){
                    Toast.makeText(mcon, "最小", Toast.LENGTH_SHORT).show();
                }else{
                    mlist.get(pos).setZan_cun_zhi(mlist.get(pos).getZan_cun_zhi()-1);
                    mlist.get(pos).setMingcheng(mslist.get(pos)+"*");
                    mbiao_ti_toolbar.setTitle(biaotiming+"*");
                    madapter.notifyItemChanged(pos);
                }

            }
        }, this);
        mkanjihualiebiaorecyclerview.setAdapter(madapter);
        mkanjihualiebiaorecyclerview.setLayoutManager(new LinearLayoutManager(this));
    }
    void reload(){
       load();
       mbiao_ti_toolbar.setTitle(msq.du_qu_xinxi().getBiao_ti());
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.kan_ji_hua_menu, menu);
        return true;
    }

    int tongjizongshijian() {
        int sum = 0;
        for (int i = 0; i < mlist.size(); i++) {
            sum += mlist.get(i).getChu_shi_zhi();
        }
        return sum;
    }

    int tongjishengyushijian() {
        int sum = 0;
        for (int i = 0; i < mlist.size(); i++) {
            sum += mlist.get(i).getZan_cun_zhi();
        }
        return sum;
    }

    int tongjiyiyongshijian() {


        return tongjizongshijian() - tongjishengyushijian();
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.hui_dang_ji_hua_item:
                sethuidang();
                break;
            case R.id.bao_cun_ji_hua_item:
                save();
                break;
            case R.id.kan_de_tong_ji_shi_jian:
                Toast.makeText(this, "总数"+tongjizongshijian()+"剩余"+tongjishengyushijian()+"已用"+tongjiyiyongshijian(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.kan_de_chu_shi_hua:
                chushihuaalertdialog();
                break;
            case R.id.kan_de_sui_ji_xuan_qu:
                JiHuaTiaoJavaBean result=chouqu();
                if(result==null) Toast.makeText(this, "都做完了", Toast.LENGTH_SHORT).show();
                else{
                    xianshijieguodialog(result);
                }
                break;
            case R.id.kan_de_bian_ji:
                Intent min=new Intent(this,BianJiJiHuaActivity.class);
                min.putExtra("wen_jian_ming",wenjianming);
                startActivityForResult(min,51);
                break;
            case R.id.kan_de_fu_zhi_bian_ji:
                Intent min2=new Intent(this,BianJiJiHuaActivity.class);
                min2.putExtra("wen_jian_ming",wenjianming);
                min2.putExtra("fu_zhi",true);
                min2.putExtra("suo_shu_id",myapp.mSuoYouJiHuaSQCaoZuo.reloadByIdJiHua(msq.du_qu_xinxi().getJi_hua_id()).getSuo_shu_ji_hua_bao_id());
                min2.putExtra("bao_han_ren_wu_shu",myapp.mSuoYouJiHuaBaoSQCaoZuo.reloadByIdJiHuaBao(myapp.mSuoYouJiHuaSQCaoZuo.reloadByIdJiHua(msq.du_qu_xinxi().getJi_hua_id()).getSuo_shu_ji_hua_bao_id()).getJi_hua_shu());
                startActivityForResult(min2,61);
                break;
            case R.id.kan_de_yi_dong_zhi_bao:
                Intent min3=new Intent(this,GuanLiJiHuaActivity.class);
                min3.putExtra("ji_hua_id",msq.du_qu_xinxi().getJi_hua_id());
                startActivityForResult(min3,71);
                break;
        }
        return true;
    }
    JiHuaTiaoJavaBean chouqu() {
        ArrayList<JiHuaTiaoJavaBean> temp = new ArrayList<JiHuaTiaoJavaBean>();
        for (int i = 0; i < mlist.size(); i++) {
            if (mlist.get(i).getZan_cun_zhi()== 0) continue;
            else {
                JiHuaTiaoJavaBean tempo=mlist.get(i);
                tempo.setMingcheng(mslist.get(i));
                temp.add(tempo);
            }
        }
        if (temp.size() > 0) {
            int num = temp.size();
            int ran2 = (int) (Math.random() * num);
            return temp.get(ran2);
        } else {
            return null;
        }
    }
    void chushihuaalertdialog(){
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("选择")
                .setMessage("是否初始化？")
                .setCancelable(false)
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        chushihua();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(myapp, "取消", Toast.LENGTH_SHORT).show();
                    }
                })
                .create();
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.lan_se));
        dialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(getResources().getColor(R.color.lan_se));


    }
    void xianshijieguodialog(JiHuaTiaoJavaBean o){
        AlertDialog dialog = new AlertDialog.Builder(this).setTitle("结果")
                .setMessage("结果是: "+o.getMingcheng())
                .setCancelable(false)
                .setPositiveButton("好", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .create();
        dialog.show();
        dialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(getResources().getColor(R.color.lan_se));

    }
    void  save(){
        for (int i = 0; i < mlist.size(); i++) {
            JiHuaTiaoJavaBean temp = mlist.get(i);
            temp.setMingcheng(mslist.get(i));
            temp.setXian_zai_zhi(temp.getZan_cun_zhi());
            mlist.set(i, temp);
        }
        mbiao_ti_toolbar.setTitle(biaotiming);
        madapter.notifyDataSetChanged();
        xierushujuku();
        Toast.makeText(myapp, "保存", Toast.LENGTH_SHORT).show();
    }
    private void chushihua() {
        for (int i = 0; i < mlist.size(); i++) {
            JiHuaTiaoJavaBean temp = mlist.get(i);
            temp.setMingcheng(mslist.get(i));
            temp.setZan_cun_zhi(temp.getChu_shi_zhi());
            temp.setXian_zai_zhi(temp.getChu_shi_zhi());
            mlist.set(i, temp);
        }
        mbiao_ti_toolbar.setTitle(biaotiming);
        madapter.notifyDataSetChanged();
        Toast.makeText(this, "初始化", Toast.LENGTH_SHORT).show();
        xierushujuku();
    }

    private void xierushujuku() {
        msq.update_suo_you(mlist);
    }

    private void sethuidang() {
        for (int i = 0; i < mlist.size(); i++) {
            JiHuaTiaoJavaBean temp = mlist.get(i);
            temp.setZan_cun_zhi(temp.getXian_zai_zhi());
            mlist.set(i, temp);
            mlist.get(i).setMingcheng(mslist.get(i));
        }
        mbiao_ti_toolbar.setTitle(biaotiming);
        madapter.notifyDataSetChanged();
        Toast.makeText(this, "回档", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==51&&resultCode==52){
            reload();
            setResult(42);
        }else if(requestCode==61&&resultCode==62){
            setResult(42);
            finish();
        }else if(requestCode==71&&resultCode==72){

            setResult(43,data);
            finish();
        }
    }
}
